/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.tb_loading_address.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.tb_areas.service.TbAreasService;
import com.thinkgem.jeesite.modules.tb_discharge_cargo_address.entity.TbDischargeCargoAddress;
import com.thinkgem.jeesite.modules.tb_loading_address.entity.TbLoadingAddress;
import com.thinkgem.jeesite.modules.tb_loading_address.service.TbLoadingAddressService;

/**
 * 装货地址管理Controller
 * @author ZhangYangZe
 * @version 2017-05-13
 */
@Controller
@RequestMapping(value = "${adminPath}/tb_loading_address/tbLoadingAddress")
public class TbLoadingAddressController extends BaseController {

	@Autowired
	private TbLoadingAddressService tbLoadingAddressService;
	@Autowired
	private TbAreasService tbAreasService;
	/**
	 * 获取单条数据
	 * @param String
	 * @return TbLoadingAddress
	 */
	@ModelAttribute
	public TbLoadingAddress get(@RequestParam(required=false) String id) {
		TbLoadingAddress entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbLoadingAddressService.get(id);
		}
		if (entity == null){
			entity = new TbLoadingAddress();
		}
		return entity;
	}
	/**
	 * 查询分页数据
	 * @param String
	 * @param TbLoadingAddress
	 * @param HttpServletRequest
	 * @param HttpServletResponse
	 * @param Model
	 * @return String
	 */
	@RequiresPermissions("tb_loading_address:tbLoadingAddress:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbLoadingAddress tbLoadingAddress, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbLoadingAddress> page = tbLoadingAddressService.findPage(new Page<TbLoadingAddress>(request, response), tbLoadingAddress); 
		model.addAttribute("page", page);
		return "modules/tb_loading_address/tbLoadingAddressList";
	}
	/**
	 * 查询下级城市（联动菜单用）
	 * @param TbLoadingAddress
	 * @param Model
	 * @return String
	 */
	@RequiresPermissions("tb_loading_address:tbLoadingAddress:view")
	@RequestMapping(value = "form")
	public String form(TbLoadingAddress tbLoadingAddress, Model model) {
		model.addAttribute("tbAreas", tbAreasService.findCityByParentId("0"));
		model.addAttribute("tbLoadingAddress", tbLoadingAddress);
		return "modules/tb_loading_address/tbLoadingAddressForm";
	}
	/**
	 * 查询下级城市（联动菜单用）
	 * @param TbLoadingAddress
	 * @param RedirectAttributes
	 * @param Model
	 * @return String
	 */
	@RequiresPermissions("tb_loading_address:tbLoadingAddress:edit")
	@RequestMapping(value = "save")
	public String save(TbLoadingAddress tbLoadingAddress, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbLoadingAddress)){
			return form(tbLoadingAddress, model);
		}
		tbLoadingAddressService.save(tbLoadingAddress);
		addMessage(redirectAttributes, "保存装货地址成功");
		return "redirect:"+Global.getAdminPath()+"/tb_loading_address/tbLoadingAddress/?repage";
	}
	/**
	 * 删除装货的地址
	 * @param TbLoadingAddress
	 * @param RedirectAttributes
	 * @return String
	 */
	@RequiresPermissions("tb_loading_address:tbLoadingAddress:edit")
	@RequestMapping(value = "delete")
	public String delete(TbLoadingAddress tbLoadingAddress, RedirectAttributes redirectAttributes) {
		tbLoadingAddressService.delete(tbLoadingAddress);
		addMessage(redirectAttributes, "删除装货地址成功");
		return "redirect:"+Global.getAdminPath()+"/tb_loading_address/tbLoadingAddress/?repage";
	}
	/**
	 * 装货的地址详情
	 * @param TbLoadingAddress
	 * @param RedirectAttributes
	 * @param HttpServletRequest
	 * @param HttpServletResponse
	 * @param Model
	 * @return String
	 */
	@RequestMapping("loadingAddressDetails")
	@ResponseBody
	public ArrayList<TbLoadingAddress> loadingAddressDetails(TbLoadingAddress tbLoadingAddress, HttpServletRequest request, HttpServletResponse response, Model model){
		String id = request.getParameter("id");
		ArrayList<TbLoadingAddress> loadingAddressDetails = tbLoadingAddressService.loadingAddressDetails(id);	
		return loadingAddressDetails;
	}
}